import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Server {

    public static void main(String[] args) {
        // s1();
        // s2();
        s3();
    }

    public static void s3() {
        try {
            System.out.println("Server start and readying!");
            ServerSocket server = new ServerSocket(8080);
            // 等客户端连接，accept()方法会阻止程序继续往下执行
            // 保存连接的客户socket对象
            ArrayList<Socket> clients = new ArrayList<>();
            while (true) {
                Socket accept = server.accept();// 只是阻塞当前程序
                clients.add(accept);
                System.out.println(accept);
                InputStream is = accept.getInputStream();
                OutputStream os = accept.getOutputStream();
                new Thread() {
                    @Override
                    public void run() {
                        if (clients.size() == 2)
                            while (true) {
                                DominoManager.Instance.Init();
                                int[] Index = DominoManager.Instance.getLibraryIndex();
                                String[] indexStrings = new String[28];
                                String pwd = "";
                                for (int i = 0; i < Index.length; i++) {
                                    indexStrings[i] = Integer.toString(Index[i]);
                                    pwd += indexStrings[i] + " ";
                                    System.out.print(indexStrings[i] + " ");
                                }
                                System.out.println();
                                System.out.println("pwd:" + pwd);
                                try {
                                    byte[] bs = new byte[1024];
                                    int len = 0;// 实际读取的内容长度
                                    // 发送给所有客户端
                                    for (Socket item : clients) {
                                        OutputStream os = item.getOutputStream();
                                        os.write(pwd.getBytes());// 发送pwd
                                    }
                                    Random random = new Random();
                                    int first = 0 + random.nextInt(2);

                                    for (Socket item : clients) {
                                        OutputStream os = item.getOutputStream();
                                        if (item.equals(clients.get(first))) {
                                            System.out.println(clients.get(first) + " is the first");
                                            os.write("You are the first".getBytes());
                                        } else {
                                            System.out.println(item + " is the second");
                                            os.write("You are the second".getBytes());
                                        }
                                    }

                                    boolean game = false;

                                    len = is.read(bs);
                                    String str = new String(bs, 0, len);
                                    if (str.equals("ready")) {
                                        game = true;
                                        System.out.println(str);
                                    }
                                    if (game) {
                                        for (Socket item : clients) {
                                            OutputStream os = item.getOutputStream();
                                            os.write("start".getBytes());// 发送pwd
                                        }
                                        System.out.println("game start");
                                        while (true) {
                                            len = is.read(bs);
                                            String r = new String(bs, 0, len);
                                            if (r.endsWith("bye")) {
                                                break;
                                            }
                                            System.out.println(r);
                                            // 发送给所有客户端
                                            for (Socket item : clients) {
                                                OutputStream os = item.getOutputStream();
                                                os.write(r.getBytes());
                                            }
                                        }

                                    }

                                    /*
                                     * if (game) {
                                     * for (Socket item : clients) {
                                     * OutputStream os = item.getOutputStream();
                                     * os.write("game start!".getBytes());// 发送pwd
                                     * }
                                     * }
                                     */

                                    /*
                                     * 
                                     * 
                                     * if (str.equals("first readying")) {
                                     * for (Socket item : clients) {
                                     * OutputStream os = item.getOutputStream();
                                     * if (!item.equals(clients.get(first)))
                                     * os.write(str.getBytes());// 发送
                                     * }
                                     * }
                                     * 
                                     * System.out.println(str);
                                     * len = is.read(bs);
                                     * str = new String(bs, 0, len);
                                     * System.out.println(str);
                                     * 
                                     * if (str.equals("second readying")) {
                                     * for (Socket item : clients) {
                                     * OutputStream os = item.getOutputStream();
                                     * os.write("game start!".getBytes());// 发送pwd
                                     * }
                                     * }
                                     * 
                                     * // String content = sc.nextLine();//阻塞
                                     * // content = "服务端：" + content;
                                     * // os.write(content.getBytes());
                                     */
                                    while (true) {
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                    }
                }.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("服务端已经启动完毕");
    }
}